
xquery version "1.0";

module namespace actmod="http://www.example.com/actmod";

declare namespace dc = "http://purl.org/dc/elements/1.1/";
declare namespace pm = "http://www.politicalmashup.nl";
declare namespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

import module namespace addf="http://www.example.com/addf" at "xmldb:exist:///db/projdb/modules/addf.xqm";
import module namespace activity="http://www.example.com/activity" at "xmldb:exist:///db/projdb/modules/activity.xqm";

(: Returns the number of meetings where this person is mentioned in the present-list OR if not available, is actor in :)
declare function actmod:_count-present($doc, $speaker)
{
    count( $doc/root[(pm:proceedings/pm:topic/pm:stage-direction[data(@pm:type) = 'present']/pm:members/@pm:speaker) = $speaker
    or data(meta/dc:relation/pm:subject/pm:actor/pm:person/@pm:speaker) = $speaker ])
};

declare function actmod:generate_activity_parameters($doc, $iandp_file, $speaker_file, $mayors_file, $onderbrekers_totalen_file, $output_collection, $output_filename) {
    (: list of speakers :)
    let $speaker_list := $speaker_file/speakers/speaker

    (: list of actors :)
    let $actors := $doc/root/meta/dc:relation/pm:subject[@xsi:type = "speaker"]/pm:actor
    
    (: create a list of all speakers in the collection (distinct) :)
    let $distinct-speakers := data($speaker_list/name)

    (: list of mayors:)
    let $mayors := $mayors_file/burgemeesters/tupel

    (: total number of meetings :)
    let $number-of-meetings := count($doc/root)

    let $interuptions := $onderbrekers_totalen_file/onderbrekerstotalen/entry 

    (: for each speaker in collection :)
    let $tupels := for $speaker at $position in $distinct-speakers

        let $initiated-topics := activity:number_of_initiated_topics($iandp_file, $speaker)
        let $participated-topics := activity:number_of_participated_topics($iandp_file, $speaker)
        let $initiatedParticipated-topics := activity:number_of_participated_initiated_topics($iandp_file, $speaker)
        
        let $present := actmod:_count-present($doc, $speaker)
        let $role := string-join(distinct-values(data($actors/pm:person[data(@pm:speaker) = $speaker]/@pm:role)), '')

        let $selected-speaker := $speaker_list[data(name) = $speaker]
        let $sex := data($selected-speaker/sex)
        let $pparty := data($selected-speaker/partij)
        let $count-interuptions := data($interuptions[data(naam) = $speaker]/aantal)
        let $speaker-ref := ($actors/pm:person[data(@pm:speaker)=$speaker]/@pm:member-ref)[1]
        let $avgNumberOfWords := activity:averageNumberOfWords($doc, $speaker)

        return element tupel {
                attribute ref {if (not ($speaker-ref) or $speaker-ref = '') then concat('be.vln.c.', $position) else $speaker-ref},
                element speaker {$speaker},
                element role {if ($speaker = 'Marc Van de Vijver') then 'mp' else $role},
                element present {$present},
                element avgNumberOfWords {$avgNumberOfWords},
                element initiatedTopics {$initiated-topics},
                element participatedTopics {$participated-topics},
                element initiatedParticipatedTopics {$initiatedParticipated-topics},
                element interuptions {if (not($count-interuptions)) then 0 else $count-interuptions},
                element mayor {if ($mayors[data(burgemeester) = $speaker]) then 'TRUE' else 'FALSE'},
                element sex {$sex},
                element pparty {$pparty}
            }
        

    let $output := element result {
        element stats {
            element numberOfMeetings { $number-of-meetings }(:,
            element numberOfValidTopics { $number-of-valid-topics }:)
        },
        $tupels
        }

    let $store-return-status := xmldb:store($output_collection, $output_filename, $output )

    return <message>New Document Created {$store-return-status} at {$output_collection}/{$output_filename}</message>
};



